const { MouseHandler } = require('../../data/mouse');
const { MouseDao } = require('../dao/mouse')

/* ======== Action Logic ============ */
const MouseAction = {
    logPos() {
        MouseHandler.logPos(event => {
            console.log('Log POS', event);
        });
    },
    clearPos() {
        MouseHandler.posStack.length = 0;
    },
    clickPos(id) {
        MouseDao.getPosById(id, row => {
            console.log('Click Pos data', id, row);
            MouseHandler.clickPosStack(JSON.parse(row.pos));
        })
    },
    stopPos() {
        MouseHandler.stopPos(() => {
            console.log('Stop POS');
            MouseDao.logPos(MouseHandler.posStack, () => {
                MouseHandler.posStack.length = 0;
            });
        })
    },
    async getPosById(id) {
        console.log('Get From MouseDao.getPosById', id);
        return new Promise(reslove => MouseDao.getPosById(id, row => reslove(row.pos)));
    },

    async getAllPos() {
        console.log('Get From MouseDao.getAllPos');
        return new Promise(reslove => MouseDao.getAllPos(rows => reslove(rows)));
    },

    delPosById(id) {
        console.log('Delete From MouseDao.delPosById', id);
        MouseDao.delPosById(id);
    },

    altPosById({ id, note }) {
        console.log('Alter From MouseDao.delPosById', id);
        MouseDao.altPosNote({ id, note });
    }

}

/* ==== Router Regist  ============ */

const MouseRouter = router => {

    router.get('/action/mouse/do/:act', async (ctx, next) => {
        console.log('Router : Ask mouse action # ', ctx.params.act);
        MouseAction[ctx.params.act]();
        ctx.body = 'OK';
        next();
    });

    router.get('/action/mouse/clicks/:id', async (ctx, next) => {
        console.log('Router : Click mouse pos by ID :# ', ctx.params.id);
        MouseAction.clickPos(ctx.params.id);
        ctx.body = 'OK';
        next();
    });

    router.get('/action/mouse/pos/:id', async (ctx, next) => {
        console.log('Router : Get mouse pos by ID :# ', ctx.params.id);
        let row = await MouseAction.getPosById(ctx.params.id);
        ctx.set('Content-Type', 'application/json');
        ctx.body = row;
        next();
    });

    router.get('/action/mouse/all', async (ctx, next) => {
        console.log('Router : Get All mouse pos# ');
        let rows = await MouseAction.getAllPos();
        ctx.set('Content-Type', 'application/json');
        ctx.body = rows;
        next();
    });

    router.delete('/action/mouse/pos/:id', async (ctx, next) => {
        console.log('Router : Delete mouse pos by ID :# ', ctx.params.id);
        MouseAction.delPosById(ctx.params.id);
        ctx.status = 204;
        ctx.body = 'OK';
        next();
    });

    router.patch('/action/mouse/pos/:id', async (ctx, next) => {
        console.log('Router : Delete mouse pos by ID :# ', ctx.params.id);
        // console.log('ctx.req',ctx.req);
        MouseAction.altPosById({ id: ctx.params.id, note:ctx.request.body.note });
        ctx.status = 201;
        ctx.body = 'OK';
        next();
    });
}

module.exports.MouseRouter = MouseRouter;